package com.canbot.u05.activity.version;

import android.content.Context;
import android.content.Intent;
import android.os.Bundle;
import android.os.Environment;
import android.view.View;
import android.widget.Button;
import android.widget.TextView;

import com.alibaba.fastjson.JSON;
import com.canbot.u05.R;
import com.canbot.u05.activity.BaseActivityNoTitle;
import com.canbot.u05.socket.IpMsgConst;
import com.canbot.u05.utils.ActivityUtil;
import com.canbot.u05.utils.FileUtil;
import com.canbot.u05.utils.Logger;
import com.canbot.u05.utils.OkHttpUtils;
import com.canbot.u05.utils.ToastTools;
import com.example.bspatch.BsPatch;

import java.io.File;
import java.util.ArrayList;
import java.util.List;

/**
 * Created by Administrator on 2018/3/7.
 */

public class NewVersionActivity extends BaseActivityNoTitle implements View.OnClickListener {

        private static final String TAG = "NewVersionActivity";


        private Context context;

        private Button btn_cancel;

        private Button btn_update;

        private TextView tv_hint1;

        private TextView tv_hint2;

        private static final String apkDir = "ai/apk/";

        private List<MobVersionResponse> needUpdateLists = new ArrayList<>();

        @Override
        protected void onCreate(Bundle savedInstanceState) {
                super.onCreate(savedInstanceState);
                context = this;
                setContentView(R.layout.activity_new_version);
                setFinishOnTouchOutside(false);
                initView();
                getVersionData();

        }

        private void initView() {
                btn_cancel = (Button) findViewById(R.id.btn_cancel);
                btn_update = (Button) findViewById(R.id.btn_update);
                btn_cancel.setOnClickListener(this);
                btn_update.setOnClickListener(this);
                tv_hint1 = (TextView) findViewById(R.id.tv_hint1);
                tv_hint2 = (TextView) findViewById(R.id.tv_hint2);
        }

        private void getVersionData() {
                Intent intent = getIntent();
                String versionInfo = intent.getStringExtra("versionInfo");
                Logger.e(TAG, "versionInfo=====" + versionInfo);
                try {
                        needUpdateLists = JSON.parseArray(versionInfo, MobVersionResponse.class);
                        tv_hint1.setText(String.format(getString(R.string.need_download_count),needUpdateLists.size()));
                        tv_hint2.setText("");
                }
                catch (Exception e) {
                        Logger.e(TAG, "e=====" + e);
                }
        }

        @Override
        public void onClick(View v) {
                int id = v.getId();
                switch (id) {
                        case R.id.btn_cancel:
                                finish();
                                break;
                        case R.id.btn_update:
                                if (needUpdateLists != null && needUpdateLists.size() > 0) {
                                        btn_update.setClickable(false);
                                        btn_cancel.setClickable(false);
                                        final MobVersionResponse mobVersionResponse = needUpdateLists.remove(0);
                                        tv_hint1.setText(getString(R.string.downloading));
                                        tv_hint2.setText("");
                                        String url = "http://" + IpMsgConst.SOCKET_HOST2 + ":7755/" + mobVersionResponse.getDownloadUrl();
                                        final String nameFromUrl = FileUtil.getNameFromUrl(url);
                                        OkHttpUtils.downloadFile(url, apkDir, nameFromUrl, new OkHttpUtils.OnDownloadListener() {
                                                @Override
                                                public void onDownloadSuccess() {
                                                        btn_cancel.setClickable(true);
                                                        btn_update.setClickable(true);
                                                        runOnUiThread(new Runnable() {
                                                                @Override
                                                                public void run() {
                                                                        tv_hint1.setText(String.format(getString(R.string.leave_download_count),needUpdateLists.size()));
                                                                        tv_hint2.setText("");
                                                                }
                                                        });

                                                        String apkPath = ActivityUtil.getApkPath(context, mobVersionResponse.getPackageName());
                                                        final String patch = Environment.getExternalStorageDirectory() + "/" + apkDir + nameFromUrl;
                                                        final String newApk = Environment.getExternalStorageDirectory() + "/" + apkDir + "new.apk";
                                                        Logger.e(TAG, "apkPath=" + apkPath);
                                                        Logger.e(TAG, "patch=" + patch);
                                                        Logger.e(TAG, "newApk=" + newApk);
                                                        if (mobVersionResponse.isPatch()) {
                                                                BsPatch.bsPatch(new File(apkPath), new File(newApk), new File(patch), new BsPatch.CallBack() {
                                                                        @Override
                                                                        public void onSucceed() {
                                                                                FileUtil.deleteFile(patch);
                                                                                ToastTools.newInstance(context).showText(getString(R.string.synthesis_finish));
                                                                                ActivityUtil.install(context.getApplicationContext(), new File(newApk));
                                                                        }

                                                                        @Override
                                                                        public void onFailed() {
                                                                                ToastTools.newInstance(context).showText(getString(R.string.synthesis_failure));
                                                                        }
                                                                });
                                                        }else {
                                                                ToastTools.newInstance(context).showText(getString(R.string.synthesis_finish));
                                                                ActivityUtil.install(context.getApplicationContext(), new File(patch));
                                                        }


                                                }

                                                @Override
                                                public void onDownloading(final int progress) {
                                                        runOnUiThread(new Runnable() {
                                                                @Override
                                                                public void run() {
                                                                        tv_hint2.setText("当前进度：" + progress + "%");
                                                                }
                                                        });

                                                }

                                                @Override
                                                public void onDownloadFailed() {
                                                        btn_cancel.setClickable(true);
                                                        btn_update.setClickable(true);
                                                        runOnUiThread(new Runnable() {
                                                                @Override
                                                                public void run() {
                                                                        tv_hint2.setText(getString(R.string.downloadFailury));
                                                                }
                                                        });

                                                }
                                        });
                                }
                                else {
                                        tv_hint1.setText(getString(R.string.lasterVersion));
                                        tv_hint2.setText("");
                                }
                                break;
                }
        }
}
